

namespace ActivUFRJ.Classes.Log
{
    public class DBLogAcessoUsuario : Connection.MySQL
    {
        public DBLogAcessoUsuario() : base(ref Connection.DBConnection.myConnection)
        {

        }

        internal bool CadastrarNovoAcesso(ActivUFRJ.Classes.Usuario.Usuario usuario)
        {
            try
            {
                bool insert = false;
                string SQL =    " INSERT INTO LOG_ACESSO_USUARIO (LAU_DATA_ACESSO, USR_ID) VALUES ('" +
                                System.DateTime.Now.ToString("s") + "', " + usuario.ID.ToString() + ")";

                insert = this.ExecuteNonQuery(SQL);
                this.Dispose();
                return insert;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        internal int VerificarNumeroDeAcessos(ActivUFRJ.Classes.Usuario.Usuario usuario)
        {
            try
            {
                int numeroAcessos = -1;
                string SQL = " SELECT COUNT(*) as numeroAcessos FROM LOG_ACESSO_USUARIO WHERE USR_ID = " + usuario.ID.ToString();

                this.SetSQL(SQL);
                if (!this.IsTableOver())
                {
                    numeroAcessos = System.Convert.ToInt32(this.TableGetValue("numeroAcessos"));
                }
                this.Dispose();
                return numeroAcessos;
            }
            catch (System.Exception ex)
            {
                return -1;
            }
        }

        internal System.DateTime VerificarUltimoAcesso(ActivUFRJ.Classes.Usuario.Usuario usuario)
        {
            try
            {
                System.DateTime ultimoAcesso = new System.DateTime();
                string SQL =    " SELECT * FROM LOG_ACESSO_USUARIO WHERE USR_ID = " + usuario.ID.ToString() +
                                " ORDER BY LAU_DATA_ACESSO DESC limit 1";

                this.SetSQL(SQL);
                if (!this.IsTableOver())
                {
                    ultimoAcesso = System.Convert.ToDateTime(this.TableGetValue("LAU_DATA_ACESSO"));
                }
                this.Dispose();
                return ultimoAcesso;
            }
            catch (System.Exception ex)
            {
                return new System.DateTime();
            }
        }
    }
}
